﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.BusinessLayer;
using SenseTimeTracker.Main;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace SenseTimeTracker.Clients
{
	public class ClientsSliderPanel : CommonSliderPanel<Client>
	{
		public ClientsSliderPanel(SenseTimeTrackerForm form)
			: base(form, "ClientSmallIcon.png", "ClientLargeIcon.png", MenuEnty.All)
		{
			sorting = Persistance.Instance.ClientsSorting;
			sortColumns = SortColumn.Name | SortColumn.Type;
		}

		public override string Name { get { return Resources.PanelClientsName; } }

		protected override void MenuItemNewEntry_Click(object sender, EventArgs e)
		{
			using (new DialogHelper(ownedForm))
			{
				ClientEditForm form = new ClientEditForm
				{
					Client = new Client(),
				};

				if (form.ShowDialog() == DialogResult.OK)
				{
					form.Client.Insert();
					Bind(senseListControl);
				}
			}
		}

		protected override void MenuItemEditEntry_Click(object sender, EventArgs e)
		{
			ClientDisplayItem item = senseListControl.FocusedItem as ClientDisplayItem;
			if (item != null)
			{
				EditClient(item.Client.Id);
				Bind(senseListControl);
			}
		}

		protected override void MenuItemCopyEntry_Click(object sender, EventArgs e)
		{
			CopyAndEditClient(false);
		}

		protected override void MenuItemDuplicateEntry_Click(object sender, EventArgs e)
		{
			CopyAndEditClient(true);
		}

		protected override void MenuItemRemoveEntry_Click(object sender, EventArgs e)
		{
			if (SenseAPIs.SenseMessageBox.Show(Resources.DialogDeleteQuestion, Resources.DialogHeaderRemoveClient, SenseMessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				ClientDisplayItem item = senseListControl.FocusedItem as ClientDisplayItem;
				if (item != null)
				{
					Client.Delete(item.Client.Id);
					Bind(senseListControl);
				}
			}
		}

		public override void Bind(SenseListControl senseListControl)
		{
			base.Bind(senseListControl);

			senseListControl.AddItem(new SensePanelDividerItem(string.Empty, Resources.ClientsExplanation));
			var filteredClients = from client in Client.Clients select client;
			SortFilter(ref filteredClients);
			foreach (Client client in filteredClients)
			{
				senseListControl.AddItem(new ClientDisplayItem
				{
					Client = client,
				});
			}
		}

		public override void BindFilter()
		{
			BindFilter(Resources.ClientsFilterHeader);
		}

		/// <summary>
		/// Method to edit a client.
		/// </summary>
		/// <param name="id">Client identifier</param>
		/// <returns>Result of the edit dialog</returns>
		private DialogResult EditClient(Guid id)
		{
			DialogResult dialogResult = DialogResult.Cancel;
			using (new DialogHelper(ownedForm))
			{
				ClientEditForm form = new ClientEditForm
				{
					Client = Client.GetClient(id),
				};

				if (form.ShowDialog() == DialogResult.OK)
				{
					form.Client.Update();
					dialogResult = DialogResult.OK;
				}
			}

			return dialogResult;
		}

		/// <summary>
		/// Method to copy and edit a client
		/// </summary>
		/// <param name="doubleClient">Copy the client before edit</param>
		private void CopyAndEditClient(bool doubleClient)
		{
			ClientDisplayItem item = senseListControl.FocusedItem as ClientDisplayItem;
			if (item != null)
			{
				Guid oldClientId = item.Client.Id;
				Guid newClientId = DatabaseManager.ClientCopy(oldClientId);
				if (EditClient(newClientId) == DialogResult.OK)
				{
					if (doubleClient)
					{
						foreach (Guid projectId in DatabaseManager.ClientAllProjects(oldClientId))
						{
							Guid newProjectId = Project.Copy(projectId, newClientId);
							foreach (Guid taskId in DatabaseManager.ProjectAllTasks(projectId))
							{
								DatabaseManager.TaskCopy(taskId, newProjectId);
							}
						}
					}
				}
				else
				{
					Client.Delete(newClientId);
				}

				Bind(senseListControl);
			}
		}
	}
}
